<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>底部导航</title>
		<script type="text/javascript" src="../script/api.js"></script>
		<script type="text/javascript" src="../script/jquery.js"></script>
		<style>
			table {
				margin-left: 5px;
				margin-right: 5px;
				width: calc(100% - 10px);
				text-align: center;
				border: 0px;
				font: caption;
			}
			table tr {
				border: 1px solid #f4f4f4;
				padding: 5px;
			}
			table tr td {
				width: 32%;
				border: 1px solid #f4f4f4;
				padding: 5px
			}
			.tr1 {
				background-color: #f4f4f4;
			}
			.one {
				width: 100%;
				border-bottom: 1px solid #eee;
				padding: 5px;
			}
			.one_zi {
				margin-left: 10px;
				margin-top: 5px;
				font-size: 12px;
			}
			body {
				background: #FFF;
			}
		</style>
	</head>
	<body >
		<input type="button" onclick="scanDevice()" value="搜索设备"/>
		<input type="button" onclick="connectedDevice()" value="连接设备"/>
		<input type="button" onclick="startNotifyDevice()" value="开始记录数据"/>
		<input type="button" onclick="stopNotifyDevice()" value="获取数据"/>
		<input type="button" onclick="stopDevice()" value="停止搜索设备"/>
		<input type="button" onclick="readDevice('fff2')" value="电量"/>
		<input type="button" onclick="readDevice('fff5')" value="电量xx"/>
		<input type="button" onclick="writeDevice('fff5')" value="data"/>
		<input type="button" onclick="stopConn()" value="stopConn"/>
		<input type="button" onclick="bbbb()" value="bbbb"/>
		<table class="grxx_top_biaoge" border="0" cellpadding="0" cellspacing="0"  style="width:95%;">
			<tr>
				<th>设备编号</th>
				<th >匹配设备</th>
			</tr>
			<tr>
				<td >腰</td>
				<td id="k_head" >未匹配</td>
			</tr>
			<tr>
				<td >膝</td>
				<td  id="k_knee">未匹配</td>
			</tr>
			<tr>
				<td >足</td>
				<td  id="k_font">未匹配</td>
			</tr>
		</table>
		<div >电量：<span id="dl"></span></div>
		<script>
			var scanDeviceModule = null;
			addr_head='A0:E6:F8:4E:B0:EC'
			addr_knee='A0:E6:F8:4C:E9:0C'
			addr_font='A0:E6:F8:50:D2:3A'
			var cur_param = {
				devices : [{
					title : "测试设备",
					addr : addr_head,
					id : "head"
				}, {
					title : "测试设备",
					addr : addr_knee,
					id : "knee"
				}, {
					title : "测试设备",
					addr : addr_font,
					id : "font"
				}]
			};
//							devices : [{
//					title : "测试设备",
//					addr : "A0:E6:F8:4C:C6:C8",
//					id : "head"
//				}, {
//					title : "测试设备",
//					addr : "A0:E6:F8:4E:C1:DF",
//					id : "knee"
//				}, {
//					title : "测试设备",
//					addr : "A0:E6:F8:4E:B9:34",
//					id : "font"
//				}]
			apiready = function() {
				$("#device_data").hide();
				scanDeviceModule = api.require('scanDeviceModule');
			}
			var uuid = null;
			cur_scan = true
			function scanDevice() {
				scanDeviceModule.scan(function(ret, err) {
					msg = ret;
				
					if (ret.status == 0) {
						if (cur_scan) {
							if (ret.addr == addr_head) {
								$("#k_head").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_head").attr("class", "matchs")
							}
							if (ret.addr == addr_knee) {
								$("#k_knee").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_knee").attr("class", "matchs")
							}
							if (ret.addr == addr_font) {
								$("#k_font").html('<span style="color:#f4c300">已匹配</span>')
								$("#k_font").attr("class", "matchs")
							}
							if ($(".matchs").length == 3) {
								cur_scan = false;
							}
						}
					} else {
						alert(ret.msg)
					}
				});
			}

			function stopDevice() {
				alert("停止查询设备");
				scanDeviceModule.stop(function(ret, err) {
					alert("设备停止");
				});
			}

			function show(idx) {
				$('.all').hide();
				$('.div' + idx).show();
				$("#device_data").show();
			}

			cur_conn = true;
			function connectedDevice() {
		//	cur_param.showToast=true;

				scanDeviceModule.connect(cur_param, function(ret, err) {
					if (ret.status == 0) {
						if (cur_conn) {
					
							if (ret.addr == addr_head) {
								$("#k_head").html('<span style="color:green;">已连接</span>')
								$("#k_head").attr("class", "conns")
							}
							if (ret.addr == addr_knee) {
								$("#k_knee").html('<span style="color:green;">已连接</span>')
								$("#k_knee").attr("class", "conns")
							}
							if (ret.addr == addr_font) {
								$("#k_font").html('<span style="color:green;">已连接</span>')
								$("#k_font").attr("class", "conns")
							}
							if ($(".conns").length == 3) {
								$("#lianjie").hide();
								$("#caishu").show();
								cur_conn = false;
							}
						}
					}else{
					alert(JSON.stringify(ret))
					}
					//$("#" + ret.addr).append("<div>" + ret.addr + ":" + ret.isConnected + "</div>")
				});
			}
	function stopConn(){
		scanDeviceModule.stopConnect(function(ret, err) {alert(JSON.stringify(ret)+"======a")})
	}
	function bbbb(){
		scanDeviceModule.b(function(ret, err) {alert(JSON.stringify(ret)+"======b")})
	}
			function startNotifyDevice() {
				var param = {
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000fff1-0000-1000-8000-00805f9b34fb"
				};
				scanDeviceModule.notify(param, function(ret, err) {
				alert(JSON.stringify(ret))
				});
			}
			function stopNotifyDevice() {
				///sysAlert("停止更新设备信息");
				var param = {
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000fff1-0000-1000-8000-00805f9b34fb"
				};
				scanDeviceModule.stopNotify(param, function(ret, err) {

					if (JSON.stringify(ret) == "{}"||ret.status==1) {
						alert("链接已断开数据丢失,请重新搜索设备进行采集！");
				
						$("#k_head").text("未匹配");
						$("#k_knee").text("未匹配");
						$("#k_font").text("未匹配");
						cur_scan=true
						cur_conn=true
					} else {
						alert(JSON.stringify(ret))
					}
				});
			}
			function writeDevice(cuid) {
				api.prompt({
					buttons : ['确定', '取消']
				}, function(ret, err) {
					if (ret.text.length == 0) {
						alert("请输入你要改变的值")
					} else {
						if (ret.text == "1" || ret.text == "2" || ret.text == "3" || ret.text == "4") {
							var param = {
								device_name : "测试设备",
								uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
								charUuid : "0000"+cuid+"-0000-1000-8000-00805f9b34fb",
								writeContent : "0" + ret.text
							};
							scanDeviceModule.write(param, function(ret, err) {
								alert(JSON.stringify(ret));
							});
						} else {
							alert("请输入1或2或3或4");
						}
					}
				});
			}
			function readDevice(cuid) {
			
				alert("读取设备电量:"+uuid);
				var param = {
					device_name : "测试设备",
					addr : uuid,
					uuid : "0000FFF0-0000-1000-8000-00805F9B34FB",
					charUuid : "0000"+cuid+"-0000-1000-8000-00805F9B34FB"
				};
				scanDeviceModule.read(param, function(ret, err) {
		
					$("#dl").append(_hex(parseInt(ret.hex.substr(2), 16)) + "%")
				});
			}
			function _hex(d) {
				if (d == 0 || d == 32768) {
					return 0;
				} else {
					return d > 32767 ? d - 65536 : d
				}
			}
		</script>
	</body>
</html>
